package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baor.dao.ServiceDao;
import com.baor.domain.LogoVo;
import com.baor.domain.ServiceVo;
import com.baor.service.ServiceService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ServiceServiceImpl implements ServiceService {

    @Autowired
    private ServiceDao serviceDao;

    @Override
    public AjaxResult<?> serviceList(Map<String, Object> params) {
        params.put("pageIndex", (int)params.get("pageSize") * ((int)(params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("serviceList",serviceDao.serviceList(params));
        res.put("pageCount",serviceDao.pageCount(params));
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> serviceInfo(ServiceVo service) {
        ServiceVo serviceInfo = serviceDao.selectById(service.getServiceId());
        return AjaxResult.success(serviceInfo);
    }

    @Override
    public AjaxResult<?> insertService(ServiceVo service) {
        int row = serviceDao.insert(service);
        if (row > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @Override
    public AjaxResult<?> updateService(ServiceVo service) {
        int row = serviceDao.updateById(service);
        if (row > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @Override
    public AjaxResult<?> deleteService(ServiceVo service) {
        service.setDelflag(1);
        int row = serviceDao.updateById(service);
        if (row > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @Override
    public AjaxResult<?> serviceSelect(Integer siteId) {
        QueryWrapper<ServiceVo> wrapper = new QueryWrapper<>();
        wrapper.eq("delflag", 0).eq("site_id", siteId);

        return AjaxResult.success(serviceDao.selectList(wrapper));
    }
}
